﻿app.controller('SignupController', function ($scope, $rootScope, signupFactory) {
    init();

    function init() {
        $scope.formData = { UserName: null, Password: null, PasswordC: null, FullName: null, Email: null, PhoneNumber: null };
        $scope.checkEmail = true;
        $scope.pageLoading = false;
        $scope.checkEmailOnblur = function () {
            var emailstring = { SearchString: $scope.formData.Email };
            signupFactory.checkEmail(emailstring).success(function (data) {
                if (data == 'true')
                    $scope.checkEmail = true;
                else {
                    $scope.checkEmail = false;
                }
            });
        };
        $scope.checkEmailFocus = function () {
            $scope.checkEmail = true;
        };


        $scope.checkUser = true;
        $scope.checkUserOnblur = function () {
            var userstring = { SearchString: $scope.formData.UserName };
            signupFactory.checkUser(userstring).success(function (data) {
                if (data == 'true')
                    $scope.checkUser = true;
                else {
                    $scope.checkUser = false;
                }
            });
        };
        $scope.checkUserFocus = function () {
            $scope.checkUser = true;
        };
        $scope.createUser = function () {
            $scope.pageLoading = true;
            if ($scope.checkUser && $scope.checkEmail) {
                signupFactory.postUser($scope.formData).success(function () {
                    $scope.pageLoading = false;
                    $scope.errorStatus = false;
                    $scope.successStatus = true;
                }).error(function () {
                    $scope.errorStatus = true;
                    $scope.successStatus = false;
                });
            }

        };
    }
});